package com.dmp.onlinestore.model.dao.testmemdb.base;

import java.util.ArrayList;
import java.util.List;
import com.dmp.onlinestore.model.dto.ProductBean;

public class InMemDatabase {

	// Number of products in the in-memory database.
	private static int DB_NUM_PRODUCTS = 53;
	
	// Database storage.
	private List<ProductBean> productList;
	
	// Hide the constructor.
	private InMemDatabase () {
		populateProducts();	
	}

	private static InMemDatabase instance = null;
	public static InMemDatabase instance() {
		if (instance == null) {
			instance = new InMemDatabase();
		}
		return instance;
	}

	private void setProductList(List<ProductBean> productList) {
		this.productList = productList;
	}
	public List<ProductBean> getProductList() {
		return this.productList;
	}
	private void populateProducts() {
		List<ProductBean> prodList = new ArrayList<ProductBean>();
		for (int i=1; i <= DB_NUM_PRODUCTS; i++) {
			int catIdx = (i%4) + 1;
			int subcatIdx = (i%3) + 1;
			ProductBean product = new ProductBean();
			product.setId(10000 + i);
			product.setName("name-" + i);
			product.setPerformers("performers-" + i);
			product.setDescShort("short-desc-" + i);
			product.setDescLong("long-desc-" + i);
			product.setCategory("category-" + catIdx);
			product.setSubcategory("subcategory-" + subcatIdx);
			product.setPrice( ((i%5) + 1) * 1.0);
			product.setRating( (i%6) + 1);
			prodList.add(product);
		}
		setProductList(prodList);
	}

	// Does a dump of the database.
	public void dump() {
		System.out.println("Products...");
	    for (ProductBean prod : getProductList()) {
	    	// Create a clone of this product object and add it to our list.
	    	System.out.println(prod.toString());
	    }
	}
}
